#include "Render/PMaterial.h"

namespace Piccolo
{
    PMaterialFactory PMaterialFactory::s_MaterialFactory{};

    bool PMaterial::HasTexture(uint32_t id) const
    {
        if (mTextures.find(id) != mTextures.end())
        {
            return true;
        }
        return false;
    }

    const TextureView* PMaterial::GetTextureView(uint32_t id) const
    {
        if (HasTexture(id))
        {
            return &mTextures.at(id);
        }
        return nullptr;
    }

    void PMaterial::SetTextureView(uint32_t id, PiccoloTexture *texture, PiccoloSampler *sampler)
    {
        if (HasTexture(id))
        {
            mTextures[id].texture = texture;
            mTextures[id].sampler = sampler;
        }
        else
        {
            mTextures[id] = { texture, sampler };
        }
        bShouldFlushResource = true;
    }

    void PMaterial::UpdateTextureViewEnable(uint32_t id, bool enable)
    {
        if (HasTexture(id))
        {
            mTextures[id].bEnable = enable;
            bShouldFlushParams = true;
        }
    }

    void PMaterial::UpdateTextureViewUVTranslation(uint32_t id, const glm::vec2 &uvTranslation)
    {
        if (HasTexture(id))
        {
            mTextures[id].uvTranslation = uvTranslation;
            bShouldFlushParams = true;
        }
    }

    void PMaterial::UpdateTextureViewUVRotation(uint32_t id, float uvRotation)
    {
        if (HasTexture(id))
        {
            mTextures[id].uvRotation = uvRotation;
            bShouldFlushParams = true;
        }
    }

    void PMaterial::UpdateTextureViewUVScale(uint32_t id, const glm::vec2 &uvScale)
    {
        if (HasTexture(id))
        {
            mTextures[id].uvScale = uvScale;
            bShouldFlushParams = true;
        }
    }
}
